# =========================================================================
# STAGE 1: 构建阶段 (Builder)
# 使用微软官方的 Playwright 镜像，它已包含 JDK 和所有依赖
# =========================================================================
FROM mcr.microsoft.com/playwright/java:v1.44.0-jammy AS builder

# 设置容器内的工作目录
WORKDIR /app

# 配置阿里云 Maven 镜像源以加速依赖下载
RUN mkdir -p /root/.m2 && \
    echo '<?xml version="1.0" encoding="UTF-8"?>' > /root/.m2/settings.xml && \
    echo '<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"' >> /root/.m2/settings.xml && \
    echo '          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' >> /root/.m2/settings.xml && \
    echo '          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0' >> /root/.m2/settings.xml && \
    echo '          http://maven.apache.org/xsd/settings-1.0.0.xsd">' >> /root/.m2/settings.xml && \
    echo '  <mirrors>' >> /root/.m2/settings.xml && \
    echo '    <mirror>' >> /root/.m2/settings.xml && \
    echo '      <id>aliyunmaven</id>' >> /root/.m2/settings.xml && \
    echo '      <mirrorOf>*</mirrorOf>' >> /root/.m2/settings.xml && \
    echo '      <name>阿里云公共仓库</name>' >> /root/.m2/settings.xml && \
    echo '      <url>https://maven.aliyun.com/repository/public</url>' >> /root/.m2/settings.xml && \
    echo '    </mirror>' >> /root/.m2/settings.xml && \
    echo '  </mirrors>' >> /root/.m2/settings.xml && \
    echo '</settings>' >> /root/.m2/settings.xml

# 拷贝 Maven 配置文件
COPY pom.xml .

# 下载 Maven 依赖，利用 Docker 缓存和阿里云镜像源
RUN --mount=type=cache,target=/root/.m2 mvn --batch-mode dependency:go-offline

# 拷贝项目的源代码
COPY src ./src

# 执行 Maven 打包命令，跳过测试
# 注意：这里我们打包成一个可分层的 JAR
RUN --mount=type=cache,target=/root/.m2 mvn --batch-mode package -DskipTests

# 使用 layertools 提取分层
RUN java -Djarmode=layertools -jar target/xhs-login-service-1.0.0.jar extract


# =========================================================================
# STAGE 2: 运行阶段 (Final Image)
# 同样使用 Playwright 镜像作为基础，因为它包含了 JRE 和所需依赖
# =========================================================================
FROM mcr.microsoft.com/playwright/java:v1.44.0-jammy

# 设置容器时区为 Asia/Shanghai (UTC+8)
ENV TZ=Asia/Shanghai

# 设置工作目录
WORKDIR /app

# 从 "builder" 阶段拷贝分层内容
COPY --from=builder /app/dependencies/ ./
COPY --from=builder /app/spring-boot-loader/ ./
COPY --from=builder /app/snapshot-dependencies/ ./
COPY --from=builder /app/application/ ./

# 声明容器对外暴露的端口
EXPOSE 8080

# 定义容器的启动命令
# 使用 JarLauncher 启动，它可以处理分层的目录结构
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]